package cn.edu.ahu.aircondition.modbus.service.domain;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * 控制Domain
 * Created by ahu on 2017/6/8.
 */
public class CtrlRegDomain {
    private Integer deviceAddress;//设备n的Address
    private Integer deviceRegAddress;//设备n控制寄存器模式地址
    private Integer deviceModelNum;//设备n控制寄存器模式地址
    private Integer deviceWindNum;//设备n控制寄存器温度地址
    private Integer deviceTempNum;//设备n控制寄存器模式地址
    private Integer deviceId;//设备n的设备ID

    private Integer deviceModelCtrl;//设备n模式
    private Integer deviceAOCCtrl;//设备n开关机
    private TreeMap<Integer, Integer> ctrlMap = new TreeMap<Integer, Integer>();




    public CtrlRegDomain(Integer n) {
        this.deviceAddress = n ;
        this.deviceRegAddress = n * 32;
        this.deviceModelNum = deviceRegAddress + 1;
        this.deviceWindNum = deviceRegAddress + 2;
        this.deviceTempNum = deviceRegAddress + 3;

        this.deviceModelCtrl = 16;
        this.deviceAOCCtrl = 0;

        ctrlMap.put(deviceModelNum,16);//模式设定
        ctrlMap.put(deviceWindNum,128);//风速设定
        ctrlMap.put(deviceTempNum,20);//温度设定
    }

    public void setDeviceId(Integer deviceId) {
        this.deviceId = deviceId;
    }

    public void setAoc(String aoc){
        if(aoc.equals("开机"))
            this.deviceAOCCtrl = 128;
        else if (aoc.equals("关机"))
            this.deviceAOCCtrl = 0;
        else
            this.deviceAOCCtrl = 0;
        ctrlMap.put(deviceModelNum,deviceAOCCtrl+deviceModelCtrl);
    }

    public void setModel(String model){
        if(model.equals("自动"))
            this.deviceModelCtrl = 16;
        else if (model.equals("制冷"))
            this.deviceModelCtrl = 8;
        else if (model.equals("制热"))
            this.deviceModelCtrl = 4;
        else if (model.equals("除湿"))
            this.deviceModelCtrl = 2;
        else if (model.equals("送风"))
            this.deviceModelCtrl = 1;
        else
            this.deviceModelCtrl = 16;
        ctrlMap.put(deviceModelNum,deviceAOCCtrl+deviceModelCtrl);
    }

    public void setTemp(String temp){
        if (Integer.valueOf(temp)<16 ||Integer.valueOf(temp)>32)
            this.ctrlMap.put(deviceTempNum,26);
        else
            this.ctrlMap.put(deviceTempNum,Integer.valueOf(temp));
    }

    public void setWind(String wind){
        if (wind.equals("自动")) {
            ctrlMap.put(deviceWindNum, 128);

        } else if (wind.equals("低风")) {
            ctrlMap.put(deviceWindNum, 4);

        } else if (wind.equals("中风")) {
            ctrlMap.put(deviceWindNum, 2);

        } else if (wind.equals("高风")) {
            ctrlMap.put(deviceWindNum, 1);
        }else {
            ctrlMap.put(deviceWindNum, 128);
        }
    }

    public Integer getDeviceId() {
        return deviceId;
    }

    public TreeMap<Integer, Integer> getCtrlMap() {
        return ctrlMap;
    }

    public Integer getDeviceAddress() {
        return deviceAddress;
    }
}
